<?php
namespace Admin\Controller;
use Admin\Controller\CommonController;
class ImportController extends CommonController {
	public function import(){
		$this->display();
	}
	public function importPushorder(){
		$this->display();
	}
	public function importEms(){
		$this->display();
	}
	public function importNuclearOrder(){
		$this->display();
	}
	public function importCheckStockInfo(){
		$this->display();
	}
	//导入快递费用明细
	public function doimportEms(){
		$m=M('EmsFee',C('DB_PRS'),C('DB_DEPOT'));
		$n=M('EmsCompany',C('DB_PRS'),C('DB_DEPOT'));
		$emsid=I('emsid');
		$ems_code=$n->where(array('id'=>$emsid))->getField('code');
		if (!empty($_FILES)) {
			$config = array(
				'maxSize'    =>    3145728,    
				'savePath'   =>    '/Import/',    
				'saveName'   =>    array('uniqid',''),    
				'exts'       =>    array('xlsx', 'xls'),    
				'autoSub'    =>    true,    
				'subName'    =>    array('date','Ymd'),);
			$upload = new \Think\Upload($config);// 实例化上传类
			$info   =   $upload->upload();    
			if(!$info) {// 上传错误提示错误信息        
				alert_json(0,$upload->getError());			
			}
			$extension=$info['import']['ext'];
			// vendor("PHPExcel.PHPExcel");
			//导入PHPExcel类库，因为PHPExcel没有用命名空间，只能inport导入
			import("Org.Util.PHPExcel");
			import("Org.Util.PHPExcel.Writer.Excel5");
			import("Org.Util.PHPExcel.Writer.Excel2007");
			import("Org.Util.PHPExcel.IOFactory.php");
			
			//创建PHPExcel对象，注意，不能少了\
			$objPHPExcel = new \PHPExcel();
			
			$file_name='./Uploads'.$info['import']['savepath'].$info['import']['savename'];
			// dump($file_name);
			// die;
			if($extension=='xlsx'){
				$objReader = \PHPExcel_IOFactory::createReader('Excel2007');
			}
			if($extension=='xls'){
				$objReader = \PHPExcel_IOFactory::createReader('Excel5');
			}
			$objPHPExcel = $objReader->load($file_name,$encode='utf-8');
			$sheet = $objPHPExcel->getSheet(0);
			$highestRow = $sheet->getHighestRow(); // 取得总行数
			$highestColumn = $sheet->getHighestColumn(); // 取得总列数
			// $sql = 'truncate table '.C('DB_DEPOT.DB_NAME').'.'.C('DB_PRS').'books';
			// $stat=M()->execute($sql);//清空表
			for($i=2;$i<=$highestRow;$i++)
			{
				$data['area']		= $objPHPExcel->getActiveSheet()->getCell("A".$i)->getValue();
				$dup=$m->where(array('area'=>$data['area']))->find();
				if($dup){
					alert_json(0,$data['area']."重复");
				}
				$data['first_weight']		= $objPHPExcel->getActiveSheet()->getCell("B".$i)->getValue();
				$data['second_weight']		= $objPHPExcel->getActiveSheet()->getCell("C".$i)->getValue();
				$data['first_price']		= $objPHPExcel->getActiveSheet()->getCell("D".$i)->getValue();
				$data['second_price']		= $objPHPExcel->getActiveSheet()->getCell("E".$i)->getValue();
				$data['first_weight_t']		= $objPHPExcel->getActiveSheet()->getCell("F".$i)->getValue();
				$data['second_weight_t']		= $objPHPExcel->getActiveSheet()->getCell("G".$i)->getValue();
				$data['first_price_t']	= $objPHPExcel->getActiveSheet()->getCell("H".$i)->getValue();
				$data['second_price_t']			= $objPHPExcel->getActiveSheet()->getCell("I".$i)->getValue();

				$data['status']			= 1;
				$data['add_time']	= time();
				$data['update_time']	= time();
				$data['examine_time']	= time();

				$data['ems_code']	= $ems_code;
				//添加
				$m->add($data);	
			}
			alert_json(1,'导入成功！');
		}else
		{
			alert_json(0,"请选择上传的文件");
		}
	}
	//导入账册
	public function importBooks(){
		$m=M('Books',C('DB_PRS'),C('DB_DEPOT'));
		if (!empty($_FILES)) {
			$config = array(
				'maxSize'    =>    3145728,    
				'savePath'   =>    '/Import/',    
				'saveName'   =>    array('uniqid',''),    
				'exts'       =>    array('xlsx', 'xls'),    
				'autoSub'    =>    true,    
				'subName'    =>    array('date','Ymd'),);
			$upload = new \Think\Upload($config);// 实例化上传类
			$info   =   $upload->upload();    
			if(!$info) {// 上传错误提示错误信息        
				$this->error($upload->getError());    
			}
			$extension=$info['import']['ext'];
			// vendor("PHPExcel.PHPExcel");
			//导入PHPExcel类库，因为PHPExcel没有用命名空间，只能inport导入
			import("Org.Util.PHPExcel");
			import("Org.Util.PHPExcel.Writer.Excel5");
			import("Org.Util.PHPExcel.Writer.Excel2007");
			import("Org.Util.PHPExcel.IOFactory.php");
			
			//创建PHPExcel对象，注意，不能少了\
			$objPHPExcel = new \PHPExcel();
			
			$file_name='./Uploads'.$info['import']['savepath'].$info['import']['savename'];
			// dump($file_name);
			// die;
			if($extension=='xlsx'){
				$objReader = \PHPExcel_IOFactory::createReader('Excel2007');
			}
			if($extension=='xls'){
				$objReader = \PHPExcel_IOFactory::createReader('Excel5');
			}
			$objPHPExcel = $objReader->load($file_name,$encode='utf-8');
			$sheet = $objPHPExcel->getSheet(0);
			$highestRow = $sheet->getHighestRow(); // 取得总行数
			$highestColumn = $sheet->getHighestColumn(); // 取得总列数
			$sql = 'truncate table '.C('DB_DEPOT.DB_NAME').'.'.C('DB_PRS').'books';
			$stat=M()->execute($sql);//清空表
			for($i=2;$i<=$highestRow;$i++)
			{
				$data['manua_id']		= $objPHPExcel->getActiveSheet()->getCell("A".$i)->getValue();
				$data['item_no']		= $objPHPExcel->getActiveSheet()->getCell("B".$i)->getValue();
				$data['item_type']		= $objPHPExcel->getActiveSheet()->getCell("C".$i)->getValue();
				$data['source_no']		= $objPHPExcel->getActiveSheet()->getCell("D".$i)->getValue();
				$data['goods_name']		= $objPHPExcel->getActiveSheet()->getCell("E".$i)->getValue();
				$data['goods_no']		= $objPHPExcel->getActiveSheet()->getCell("F".$i)->getValue();
				$data['goods_spec']		= $objPHPExcel->getActiveSheet()->getCell("G".$i)->getValue();
				$data['current_type']	= $objPHPExcel->getActiveSheet()->getCell("H".$i)->getValue();
				$data['source']			= $objPHPExcel->getActiveSheet()->getCell("I".$i)->getValue();
				$data['unit1']			= $objPHPExcel->getActiveSheet()->getCell("J".$i)->getValue();
				$data['unit2']			= $objPHPExcel->getActiveSheet()->getCell("K".$i)->getValue();
				$data['add_time']		= time();
				$data['manua_type']		= $objPHPExcel->getActiveSheet()->getCell("L".$i)->getValue();
				$data['hs_code']		= $objPHPExcel->getActiveSheet()->getCell("M".$i)->getValue();
				
				$data['status']			= 1;
				$data['update_time']	= time();
				$data['examine_time']	= time();

				//添加
				$m->add($data);	
			}
			$this->success('导入成功！');
		}else
		{
			$this->error("请选择上传的文件");
		}
	}
	//导入详细库存
	public function importArrivalOrder(){
		$m=M('ArrivalOrder',C('DB_PRS'),C('DB_DEPOT'));
		if (!empty($_FILES)) {
			$config = array(
				'maxSize'    =>    3145728,    
				'savePath'   =>    '/Import/',    
				'saveName'   =>    array('uniqid',''),    
				'exts'       =>    array('xlsx', 'xls'),    
				'autoSub'    =>    true,    
				'subName'    =>    array('date','Ymd'),);
			$upload = new \Think\Upload($config);// 实例化上传类
			$info   =   $upload->upload();    
			if(!$info) {// 上传错误提示错误信息        
				$this->error($upload->getError());    
			}
			$extension=$info['import']['ext'];
			// vendor("PHPExcel.PHPExcel");
			//导入PHPExcel类库，因为PHPExcel没有用命名空间，只能inport导入
			import("Org.Util.PHPExcel");
			import("Org.Util.PHPExcel.Writer.Excel5");
			import("Org.Util.PHPExcel.Writer.Excel2007");
			import("Org.Util.PHPExcel.IOFactory.php");
			
			//创建PHPExcel对象，注意，不能少了\
			$objPHPExcel = new \PHPExcel();
			
			$file_name='./Uploads'.$info['import']['savepath'].$info['import']['savename'];
			// dump($file_name);
			// die;
			if($extension=='xlsx'){
				$objReader = \PHPExcel_IOFactory::createReader('Excel2007');
			}
			if($extension=='xls'){
				$objReader = \PHPExcel_IOFactory::createReader('Excel5');
			}
			$objPHPExcel = $objReader->load($file_name,$encode='utf-8');
			$sheet = $objPHPExcel->getSheet(0);
			$highestRow = $sheet->getHighestRow(); // 取得总行数
			$highestColumn = $sheet->getHighestColumn(); // 取得总列数
			$sql = 'truncate table '.C('DB_DEPOT.DB_NAME').'.'.C('DB_PRS').'arrival_order';
			$stat=M()->execute($sql);//清空表
			for($i=2;$i<=$highestRow;$i++)
			{
				$data['order_no']		= $objPHPExcel->getActiveSheet()->getCell("A".$i)->getValue();
				$data['batch_no']		= $objPHPExcel->getActiveSheet()->getCell("B".$i)->getValue();
				$data['manual_id']		= $objPHPExcel->getActiveSheet()->getCell("C".$i)->getValue();
				$data['goods_no']		= $objPHPExcel->getActiveSheet()->getCell("D".$i)->getValue();
				$data['goods_name']		= $objPHPExcel->getActiveSheet()->getCell("E".$i)->getValue();
				
				$data['goods_spec']		= $objPHPExcel->getActiveSheet()->getCell("F".$i)->getValue();
				$data['unit1']			= $objPHPExcel->getActiveSheet()->getCell("G".$i)->getValue();
				$data['unit2']			= $objPHPExcel->getActiveSheet()->getCell("H".$i)->getValue();
				$data['storage_amount']	= $objPHPExcel->getActiveSheet()->getCell("I".$i)->getValue();
				// $data['declare_amount']	= $objPHPExcel->getActiveSheet()->getCell("J".$i)->getValue();
				$data['receive_amount']	= $objPHPExcel->getActiveSheet()->getCell("I".$i)->getValue();
				$data['price']			= $objPHPExcel->getActiveSheet()->getCell("J".$i)->getValue();
				$data['total_price']	= $data['price']*$data['storage_amount'];
				
				$data['country_code']	= $objPHPExcel->getActiveSheet()->getCell("K".$i)->getValue();
				$data['currency']		= $objPHPExcel->getActiveSheet()->getCell("L".$i)->getValue();
				$data['bar_code']		= $objPHPExcel->getActiveSheet()->getCell("M".$i)->getValue();
				$data['conforming_p']	= $objPHPExcel->getActiveSheet()->getCell("N".$i)->getValue();
				$data['nonconforming_p']= $objPHPExcel->getActiveSheet()->getCell("O".$i)->getValue();
				$data['scrap_one']		= $objPHPExcel->getActiveSheet()->getCell("P".$i)->getValue();
				$data['overdue_time']	= $objPHPExcel->getActiveSheet()->getCell("Q".$i)->getValue();
				$data['erp_code']	= $objPHPExcel->getActiveSheet()->getCell("R".$i)->getValue();
				
				$data['status']			= 1;
				$data['inbound_status']	= 1;
				// $data['arrival_time']	= 1;
				// $data['old_goods_id']	= 1;
				$data['add_time']		= time();
				$data['update_time']	= time();
				$data['examine_time']	= time();

				$amount_s=$objPHPExcel->getActiveSheet()->getCell("I".$i)->getValue();
				$price_s=$objPHPExcel->getActiveSheet()->getCell("J".$i)->getValue();
				if(is_null($amount_s)){
					$this->error('库存不能为空');
				}
				if(is_null($price_s)){
					$this->error('单价不能为空');
				}
				if(is_null($data['erp_code'])){
					$this->error('企业编码不能为空');
				}
				//添加
				$res=$m->add($data);
				if(!$res){
					$this->error('导入错误，请查看导入数据是否完整！');
				}
			}
			$this->success('导入成功！');
		}else
		{
			$this->error("请选择上传的文件");
		}
	}
	//导入订单
	public function doimportPushorder(){
		$m=M('Pushorder',C('DB_PRS'),C('DB_DEPOT'));
		$g=M('Pushordergoods','tz_',C('DB_DEPOT'));
		$p=M('Printorder','tz_',C('DB_DEPOT'));
		if (!empty($_FILES)) {
			$config = array(
				'maxSize'    =>    3145728,    
				'savePath'   =>    '/Import/',    
				'saveName'   =>    array('uniqid',''),    
				'exts'       =>    array('xlsx', 'xls'),    
				'autoSub'    =>    true,    
				'subName'    =>    array('date','Ymd'),);
			$upload = new \Think\Upload($config);// 实例化上传类
			$info   =   $upload->upload();    
			if(!$info) {// 上传错误提示错误信息        
				$this->error($upload->getError());    
			}
			$extension=$info['import']['ext'];
			// vendor("PHPExcel.PHPExcel");
			//导入PHPExcel类库，因为PHPExcel没有用命名空间，只能inport导入
			import("Org.Util.PHPExcel");
			import("Org.Util.PHPExcel.Writer.Excel5");
			import("Org.Util.PHPExcel.Writer.Excel2007");
			import("Org.Util.PHPExcel.IOFactory.php");
			
			//创建PHPExcel对象，注意，不能少了\
			$objPHPExcel = new \PHPExcel();
			
			$file_name='./Uploads'.$info['import']['savepath'].$info['import']['savename'];
			// dump($file_name);
			// die;
			if($extension=='xlsx'){
				$objReader = \PHPExcel_IOFactory::createReader('Excel2007');
			}
			if($extension=='xls'){
				$objReader = \PHPExcel_IOFactory::createReader('Excel5');
			}
			$objPHPExcel = $objReader->load($file_name,$encode='utf-8');
			$sheet = $objPHPExcel->getSheet(0);
			$highestRow = $sheet->getHighestRow(); // 取得总行数
			$highestColumn = $sheet->getHighestColumn(); // 取得总列数
			$sql = 'truncate table '.C('DB_DEPOT.DB_NAME').'.'.C('DB_PRS').'arrival_order';
			$stat=M()->execute($sql);//清空表
			for($i=2;$i<=$highestRow;$i++)
			{	
				$data['order_no']=$order_no		= $objPHPExcel->getActiveSheet()->getCell("A".$i)->getValue();
				if($order_no!=$old_order_no){//不重复
					//重复判断--beging
					$m_info=$m->where(array('order_no'=>$order_no))->find();
					if(!empty($m_info)){
						$return['status']=1;
						$return['message']="要导入的订单编号已经存在！";
						$this->ajaxReturn($return);
					}
					//重复判断-end
				}
				
				$data['user_name']		= $objPHPExcel->getActiveSheet()->getCell("B".$i)->getValue();
				$data['user_phone']		= $objPHPExcel->getActiveSheet()->getCell("C".$i)->getValue();
				$data['user_idcard']	= $objPHPExcel->getActiveSheet()->getCell("D".$i)->getValue();
				$data['province']		= $objPHPExcel->getActiveSheet()->getCell("E".$i)->getValue();
				
				$data['city']			= $objPHPExcel->getActiveSheet()->getCell("F".$i)->getValue();
				$data['area']			= $objPHPExcel->getActiveSheet()->getCell("G".$i)->getValue();
				$data['user_address']	= $objPHPExcel->getActiveSheet()->getCell("H".$i)->getValue();
				$data['logistics_no']	= $objPHPExcel->getActiveSheet()->getCell("I".$i)->getValue();
				$data['logistics_business']	= $objPHPExcel->getActiveSheet()->getCell("J".$i)->getValue();
				
				$data['platform_name']	= $objPHPExcel->getActiveSheet()->getCell("K".$i)->getValue();
				$data['company_name']	= $objPHPExcel->getActiveSheet()->getCell("L".$i)->getValue();

				$data['add_time']		= time();

				
				$data_og['goods_no']	= $objPHPExcel->getActiveSheet()->getCell("X".$i)->getValue();
				$data_og['goods_name']	= $objPHPExcel->getActiveSheet()->getCell("Y".$i)->getValue();
				$data_og['goods_nums']	= $objPHPExcel->getActiveSheet()->getCell("Z".$i)->getValue();
				$data_og['goods_price']	= $objPHPExcel->getActiveSheet()->getCell("AA".$i)->getValue();
				$data_og['spec']	= $objPHPExcel->getActiveSheet()->getCell("AB".$i)->getValue();
				$data_og['source']	= $objPHPExcel->getActiveSheet()->getCell("AC".$i)->getValue();
				$data_og['hs_code']	= $objPHPExcel->getActiveSheet()->getCell("AD".$i)->getValue();
				$data_og['bar_code']	= $objPHPExcel->getActiveSheet()->getCell("AE".$i)->getValue();
				$data_og['goods_unit']	= $objPHPExcel->getActiveSheet()->getCell("AF".$i)->getValue();
				
				
				
				$print_order['destcode']=$objPHPExcel->getActiveSheet()->getCell("M".$i)->getValue();
				$print_order['pkgCode']=$objPHPExcel->getActiveSheet()->getCell("N".$i)->getValue();
				$print_order['sendPostcode']=$objPHPExcel->getActiveSheet()->getCell("O".$i)->getValue();
				$print_order['logistics_business']=$objPHPExcel->getActiveSheet()->getCell("J".$i)->getValue();
				$print_order['receiveMan']=$objPHPExcel->getActiveSheet()->getCell("B".$i)->getValue();
				$print_order['receiveManPhone']=$objPHPExcel->getActiveSheet()->getCell("C".$i)->getValue();
				$print_order['receiveProvince']=$objPHPExcel->getActiveSheet()->getCell("E".$i)->getValue();
				$print_order['receiveCity']=$objPHPExcel->getActiveSheet()->getCell("F".$i)->getValue();
				$print_order['receiveCounty']=$objPHPExcel->getActiveSheet()->getCell("G".$i)->getValue();
				$print_order['receiveManAddress']=$objPHPExcel->getActiveSheet()->getCell("H".$i)->getValue();
				$print_order['custid']=$objPHPExcel->getActiveSheet()->getCell("P".$i)->getValue();
				$print_order['pay_methods']=$objPHPExcel->getActiveSheet()->getCell("Q".$i)->getValue();
				$print_order['sendMan']=$objPHPExcel->getActiveSheet()->getCell("R".$i)->getValue();
				$print_order['sendManPhone']=$objPHPExcel->getActiveSheet()->getCell("S".$i)->getValue();
				$print_order['sendManAddress']=$objPHPExcel->getActiveSheet()->getCell("T".$i)->getValue();

				$print_order['sendProvince']=$objPHPExcel->getActiveSheet()->getCell("U".$i)->getValue();
				$print_order['sendCity']=$objPHPExcel->getActiveSheet()->getCell("V".$i)->getValue();
				$print_order['sendCounty']=$objPHPExcel->getActiveSheet()->getCell("W".$i)->getValue();
				$print_order['txLogisticID']=$objPHPExcel->getActiveSheet()->getCell("A".$i)->getValue();
				$print_order['mailNo']=$objPHPExcel->getActiveSheet()->getCell("I".$i)->getValue();
				$print_order['order_no']=$objPHPExcel->getActiveSheet()->getCell("A".$i)->getValue();
				
				$print_order['add_time']=time();//添加时间
				
				if($order_no!=$old_order_no){//不重复
					//添加订单
					$insert_id=$m->add($data);
					//添加订单详情
					$data_og['order_id']=$insert_id;
					$g->add($data_og);
					//添加面单
					$p->add($print_order);
				}else{//重复
					//添加订单详情
					$data_og['order_id']=$insert_id;
					$g->add($data_og);
				}
				$old_order_no=$order_no;
			}
			$return['status']=1;
			$return['message']="导入成功！";
			$this->ajaxReturn($return);
		}else
		{
			$return['status']=0;
			$return['message']="请选择上传的文件!";
			$this->ajaxReturn($return);
		}
	}
	//上传文件并返回文件名
	public function upload_file(){
		if (!empty($_FILES)) {
			$config = array(
				'maxSize'    =>    3145728,    
				'savePath'   =>    '/Import/',    
				'saveName'   =>    array('uniqid',''),    
				'exts'       =>    array('xlsx', 'xls'),    
				'autoSub'    =>    true,    
				'subName'    =>    array('date','Ymd'),);
			$upload = new \Think\Upload($config);// 实例化上传类
			$info   =   $upload->upload();    
			if(!$info) {// 上传错误提示错误信息        
				$this->error($upload->getError());    
			}
			$extension=$info['import']['ext'];
			$file_name='./Uploads'.$info['import']['savepath'].$info['import']['savename'];
			
			$return['extension']=$extension;
			$return['file_name']=$file_name;
			return $return;
		}else{
			$return['status']=0;
			$return['message']="请选择上传的文件!";
			$this->ajaxReturn($return);
		}
	}
	//导入核放单
	public function doimportNuclearorder(){
			$trans = M('',C('DB_PRS'),C('DB_DEPOT'));
			$trans->startTrans();   // 开启事务
			$m=M('NuclearOrder',C('DB_PRS'),C('DB_DEPOT'));
			$upfile=$this->upload_file();
			$extension=$upfile['extension'];
			$file_name=$upfile['file_name'];
			
			// vendor("PHPExcel.PHPExcel");
			//导入PHPExcel类库，因为PHPExcel没有用命名空间，只能inport导入
			import("Org.Util.PHPExcel");
			import("Org.Util.PHPExcel.Writer.Excel5");
			import("Org.Util.PHPExcel.Writer.Excel2007");
			import("Org.Util.PHPExcel.IOFactory.php");
			
			//创建PHPExcel对象，注意，不能少了\
			$objPHPExcel = new \PHPExcel();

			if($extension=='xlsx'){
				$objReader = \PHPExcel_IOFactory::createReader('Excel2007');
			}
			if($extension=='xls'){
				$objReader = \PHPExcel_IOFactory::createReader('Excel5');
			}
			
			$objPHPExcel = $objReader->load($file_name,$encode='utf-8');
			
			$sheet = $objPHPExcel->getSheet(0);
			$highestRow = $sheet->getHighestRow(); // 取得总行数
			$highestColumn = $sheet->getHighestColumn(); // 取得总列数
			for($i=2;$i<=$highestRow;$i++)
			{
				$data=array();
				$data['order_no']=$order_no		= $objPHPExcel->getActiveSheet()->getCell("B".$i)->getValue();
				if(empty($order_no)){
					$trans->rollback();
					$return['status']=0;
					$return['message']=$i."行，订单编号不能为空，导入失败！";
					$this->ajaxReturn($return);
				}		 
				if($order_no!=$old_order_no){//不重复
					//重复判断--beging
					$m_info=$m->where(array('order_no'=>$order_no))->find();
					if(!empty($m_info)){
						continue;
					}
					//重复判断-end
				}else{
					continue;
				}
				
				$data['listing_no']		= $objPHPExcel->getActiveSheet()->getCell("A".$i)->getValue();
				if(empty($data['listing_no'])){
					$trans->rollback();
					$return['status']=0;
					$return['message']=$i."行，申报单号不能为空，导入失败！";
					$this->ajaxReturn($return);
				}		
				$data['mail_no']		= $objPHPExcel->getActiveSheet()->getCell("C".$i)->getValue();
				if(empty($data['mail_no'])){
					$trans->rollback();
					$return['status']=0;
					$return['message']=$i."行，面单号不能为空，导入失败！";
					$this->ajaxReturn($return);
				}		
				$data['declare_time']	= $objPHPExcel->getActiveSheet()->getCell("D".$i)->getValue();
				if($data['declare_time'] instanceof PHPExcel_RichText) {    //富文本转换字符串    
						$data['declare_time'] = $data['declare_time']->__toString();
				}
				$data['declare_time']=strtotime($data['declare_time']);
				$data['release_time']	= $objPHPExcel->getActiveSheet()->getCell("E".$i)->getValue();
				if($data['release_time'] instanceof PHPExcel_RichText) {    //富文本转换字符串    
						$data['release_time'] = $data['release_time']->__toString();
				}
				$data['release_time']=strtotime($data['release_time']);

				$data['add_time']		= time();

				$insert_id=$m->add($data);
				if(!insert_id){
					$trans->rollback();
					$return['status']=0;
					$return['message']="导入失败！";
					$this->ajaxReturn($return);
				}
				$old_order_no=$order_no;
			}
			$trans->commit();
			$return['status']=1;
			$return['message']="导入成功！";
			$this->ajaxReturn($return);
	}
	//导入盘库详情
	public function doimportCheckstockinfo(){
			$trans = M('',C('DB_PRS'),C('DB_DEPOT'));
			$trans->startTrans();   // 开启事务
			$m=M('CheckstockInfo',C('DB_PRS'),C('DB_DEPOT'));
			$upfile=$this->upload_file();
			$extension=$upfile['extension'];
			$file_name=$upfile['file_name'];
			
			$order_id=I('order_id');
			
			// vendor("PHPExcel.PHPExcel");
			//导入PHPExcel类库，因为PHPExcel没有用命名空间，只能inport导入
			import("Org.Util.PHPExcel");
			import("Org.Util.PHPExcel.Writer.Excel5");
			import("Org.Util.PHPExcel.Writer.Excel2007");
			import("Org.Util.PHPExcel.IOFactory.php");
			
			//创建PHPExcel对象，注意，不能少了\
			$objPHPExcel = new \PHPExcel();

			if($extension=='xlsx'){
				$objReader = \PHPExcel_IOFactory::createReader('Excel2007');
			}
			if($extension=='xls'){
				$objReader = \PHPExcel_IOFactory::createReader('Excel5');
			}
			
			$objPHPExcel = $objReader->load($file_name,$encode='utf-8');
			
			$sheet = $objPHPExcel->getSheet(0);
			$highestRow = $sheet->getHighestRow(); // 取得总行数
			$highestColumn = $sheet->getHighestColumn(); // 取得总列数
			$data=array();
			for($i=2;$i<=$highestRow;$i++)
			{
				$data[$i-2]['manua_id']				= $objPHPExcel->getActiveSheet()->getCell("A".$i)->getValue();
				$data[$i-2]['bar_code']				= $objPHPExcel->getActiveSheet()->getCell("B".$i)->getValue();
				$data[$i-2]['goods_name']			= $objPHPExcel->getActiveSheet()->getCell("C".$i)->getValue();
				$data[$i-2]['check_amount']			= $objPHPExcel->getActiveSheet()->getCell("D".$i)->getValue();
				$data[$i-2]['check_nonconforming_p']= $objPHPExcel->getActiveSheet()->getCell("E".$i)->getValue();
				$data[$i-2]['check_scrap_one']		= $objPHPExcel->getActiveSheet()->getCell("F".$i)->getValue();
			}
			// dump($data);
			foreach($data as $k=>$v){
				foreach($v as $ks=>$vs){
					if($vs instanceof PHPExcel_RichText){
						$data[$k][$ks]=$vs->__toString();	
					}
					$data[$k][$ks]=trim($data[$k][$ks]);
					if(empty($data[$k][$ks])){
						$data[$k][$ks]=0;
					}
				}
			}
			// dump($data);
			foreach($data as $k=>$v){
				$map=array();
				$map['manua_id']=$v['manua_id'];
				$map['bar_code']=$v['bar_code'];
				$map['order_id']=$order_id;
				
				$data_arr=array();
				$data_arr['check_amount']			=$v['check_amount'];
				$data_arr['check_nonconforming_p']	=$v['check_nonconforming_p'];
				$data_arr['check_scrap_one']		=$v['check_scrap_one'];
				$res=$m->where($map)->save($data_arr);
				// echo $m->_sql();
				
				if($res===false){
					$trans->rollback();
					$return['status']=0;
					$return['message']="导入失败！";
					$this->ajaxReturn($return);
				}
			}
			$trans->commit();
			$return['status']=1;
			$return['message']="导入成功！";
			$this->ajaxReturn($return);
	}
}